#include "ClockChart.h"
#include <QDebug>
#include <QApplication>
#include <QGraphicsDropShadowEffect>
ClockChart::ClockChart(QWidget *parent)
    : QWidget(parent),
    m_radius(100),
    m_isPaused(false),
    m_mode(Countdown),
    m_initialTime(0),
    m_themeColor(Light)
{
    // 设置窗口标志为无边框、始终显示在顶层，并设置背景透明
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground, true);

    // 设置固定大小，根据半径计算
    setFixedSize(m_radius * 2, m_radius * 2);

    // 初始化定时器
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &ClockChart::updateTime);
    m_timer->start(1000); // 每秒更新一次

    // 初始化时间
    m_currentTime = QTime(0, 0, 0);

    // 初始化暂停按钮
    setupPauseButton();
}

ClockChart::~ClockChart()
{
    // 确保在对象销毁时停止定时器
    m_timer->stop();
}

// 设置时钟半径
void ClockChart::setRadius(int radius)
{
    // 确保半径为正数
    if (radius > 0) {
        m_radius = radius;
        setFixedSize(m_radius * 2, m_radius * 2);
        updateButtonPositions();
        update();
    }
}
// 设置更新间隔（参数：毫秒）
void ClockChart::setUpdateInterval(int interval)
{
    // 确保间隔为正数
    if (interval > 0) {
        m_timer->setInterval(interval);
    }
}

void ClockChart::setTimerMode(TimerMode mode)
{
    m_mode = mode;
    resetTime();
}

void ClockChart::setThemeColor(ThemeColor theme)
{
    m_themeColor = theme;
    update();
}

void ClockChart::setInitialTime(int seconds)
{
    m_initialTime = seconds;
    m_currentTime = QTime(0, 0).addSecs(m_initialTime); // 根据起始秒数初始化时间
    update();
}

void ClockChart::setupPauseButton()
{
    m_pauseButton = new QPushButton(this);
    m_pauseButton->setIcon(QIcon(":/resource/svg/stop_white.svg"));
    m_pauseButton->setIconSize(QSize(m_radius, m_radius));
    m_pauseButton->setFixedSize(m_radius, m_radius);
    m_pauseButton->setStyleSheet("QPushButton { border: none; padding: 0px; margin: 0px; }");
    m_pauseButton->setVisible(false);
    connect(m_pauseButton, &QPushButton::clicked, this, &ClockChart::togglePause);
}

void ClockChart::resetTime()
{
    // 重置时间并启动定时器
    m_currentTime = QTime(0, 0).addSecs(m_initialTime);
    m_timer->start();
    update();
}

void ClockChart::updateButtonPositions()
{
    int buttonSize = m_radius;
    m_pauseButton->setFixedSize(buttonSize, buttonSize);
    m_pauseButton->setIconSize(QSize(buttonSize, buttonSize));
    m_pauseButton->move((width() - buttonSize) / 2, (height() - buttonSize) / 2);
}


void ClockChart::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制圆形背景
    QColor bgColor = (m_themeColor == Dark) ? QColor(33, 33, 33) : QColor(255, 255, 255);
    painter.setBrush(bgColor);
    painter.setPen(Qt::NoPen); // 去掉边框
    painter.drawEllipse(0, 0, width(), height());

    // 绘制时间
    QFont font("Arial", m_radius / 2, QFont::Bold);
    painter.setFont(font);
    QColor textColor = (m_themeColor == Dark) ? Qt::white : Qt::black;
    painter.setPen(textColor);
    QString timeText = m_currentTime.hour() > 0 ?
                           m_currentTime.toString("hh:mm") :
                           m_currentTime.toString("mm:ss");
    painter.drawText(rect(), Qt::AlignCenter, timeText);

    // 如果按钮可见，绘制半透明背景
    if (m_pauseButton->isVisible()) {
        painter.setBrush(QColor(0, 0, 0, 128));  // 半透明黑色背景
        painter.setPen(Qt::NoPen); // 去掉边框
        painter.drawEllipse(0, 0, width(), height()); // 绘制圆形半透明背景
    }
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void ClockChart::enterEvent(QEnterEvent *event)
#else
void ClockChart::enterEvent(QEvent *event)
#endif
{
    Q_UNUSED(event);
    m_pauseButton->setVisible(true);
    updateButtonPositions();
    update();
}

void ClockChart::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    m_pauseButton->setVisible(false);
    update();
}

void ClockChart::updateTime()
{
    if (!m_isPaused) {
        if (m_mode == Countdown) {
            m_currentTime = m_currentTime.addSecs(-1);
            if (m_currentTime == QTime(0, 0)) {
                m_timer->stop();
            }
        } else {
            m_currentTime = m_currentTime.addSecs(1);
        }
        update();
    }
}

void ClockChart::togglePause()
{
    m_isPaused = !m_isPaused;
    emit sigClockStatus(m_isPaused);
    QString iconPath = m_isPaused ? ":/resource/svg/start_white.svg" : ":/resource/svg/stop_white.svg";
    m_pauseButton->setIcon(QIcon(iconPath));
}


